package com.example.day02.controller;

import com.example.day02.common.Result;
import com.example.day02.entity.User;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/user")
public class UserController {
    private static Map<Long, User> userMap = new HashMap<>();

    @GetMapping("/list")
    public Result<List<User>> getAllUser() {
        for (int i = 0; i < 10; i++) {
            User user = new User();
            user.setId(System.currentTimeMillis());
            user.setName("用户" + i);
            user.setLoginId("loginId" + i);
            user.setPassword("password" + i);
            user.setQq("qq" + i);
            user.setMobile("mobile" + i);
            user.setEmail("email" + i);
            user.setHeadImg("headImg" + i);
            user.setJob("job" + i);
            user.setSort(i);
            user.setRemark("remark" + i);
            user.setState(i % 2);
            user.setAddUserId(1L);
            user.setAddTime(new java.util.Date());
            user.setUpdateUserId(1L);
            user.setUpdateTime(new java.util.Date());
            userMap.put(user.getId(), user);
        }
        return Result.success(userMap.values().stream().toList());
    }
    @GetMapping("/{id}")
    public Result<User> getUser(@PathVariable Long id) {
        User user = userMap.get(id);
        if (user == null) {
            return Result.error("用户不存在");
        }
        return Result.success(user);
    }

    @PostMapping
    public Result<String> addUser(@RequestBody User user) {
        user.setId(System.currentTimeMillis()); // 模拟生成ID
        userMap.put(user.getId(), user);
        return Result.success("添加成功");
    }
}
